<template>
	<div>
		<div class="Abox" v-loading="loading">
			<Info
				ref="Info"
				v-model="infoData"
				v-bind="querys"
				state="readonly"></Info>
			<Contract
				ref="Contract"
				v-model="infoData"
				:state="ContractState || state"
				v-bind="querys"></Contract>
			<Footer
				ref="Footer"
				:state="FooterState || state"
				v-model="infoData"
				v-bind="querys"></Footer>
			<!-- <el-button -->
			<!-- class="orderChangeHistory" -->
			<!-- type="primary" -->
			<!-- @click="toOrderChangeHistory"> -->
			<!-- 查看变更历史记录 -->
			<!-- </el-button> -->
			<div class="handle">
				<el-button @click="back">返回</el-button>
				<el-button type="primary" @click="submit">提交</el-button>
			</div>
		</div>
	</div>
</template>

<script>
	import * as apis from '@/api/order/mainOrder/contractChange/api.js';
	import Info from '../chunk/info/index.vue';
	import Contract from '../chunk/contract/index.vue';
	import Footer from '../chunk/footer/index.vue';
	export default {
		name: 'ContractChange',
		props: {
			state: {
				required: false,
			},
		},
		dicts: ['order_change_type'],
		components: { Info, Contract, Footer },
		data() {
			return {
				infoData: {
					auditContent: {},
				},
				ContractState: undefined,
				FooterState: undefined,
				querys: {},
				loading: false,
			};
		},

		computed: {},
		created() {
			this.querys = this.$route.query ?? {};
			if (this.querys.from == 'relaunch') {
				this.ContractState = 'readonly';
				this.FooterState = 'write';
				this.getRelaunchInfo();
				return;
			}
			this.getInfo();
		},
		mounted() {},
		methods: {
			getRelaunchInfo() {
				if (this.Empty(this.querys.orderChangeId)) {
					this.$message.error('缺少参数 订单改变id');
					return;
				}
				this.loading = true;
				apis.OrderChangeInfo(this.querys.orderChangeId)
					.then(res => {
						if (res.code == 200) {
							this.infoData = res.data ?? {};
							this._onload();
						}
					})
					.finally(() => {
						this.loading = false;
					});
			},
			getInfo() {
				if (this.Empty(this.querys.orderId)) {
					this.$message.error('缺少参数 订单id');
					return;
				}
				this.loading = true;
				apis.OrderInfo(this.querys.orderId)
					.then(res => {
						if (res.code == 200) {
							this.infoData = res.data ?? {};

							this._onload();
						}
					})
					.finally(() => {
						this.loading = false;
					});
			},
			onload() {
				this.initDefaultData();
			},
			initDefaultData() {
				this.infoData.orderId ??= this.querys.orderId;
				this.infoData.changeType ??=
					this.dict.type.order_change_type?.[0]?.value;
				this.infoData.auditContent ??= {};
				this.$nextTick(() => {
					this._clearValid();
				});
			},
			toOrderChangeHistory() {
				this._toPage({
					name: 'OrderChangeHistory',
					query: this.querys,
				});
			},
			back() {
				this._closeCurrentPage();
				this._toPage({
					name: 'OrderView',
				});
			},
			async submit() {
				// console.log(this.infoData);
				let pass = await this._validate();

				if (!pass) {
					this.scrollToError();
					return;
				}
				let params = this._.cloneDeep(this.infoData);
				if (this.Empty(params.auditContent)) {
					params.auditContent = null;
				}
				this.$modal.loading('正在提交数据，请稍后...');
				apis.Submit(params)
					.then(res => {
						if (res.code == 200) {
							this.$message({
								type: 'success',
								message: '提交成功',
							});
							this.back();
						} else {
							this.$message({
								type: 'error',
								message: res.msg,
							});
						}
					})
					.finally(() => {
						this.$modal.closeLoading();
					});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.orderChangeHistory {
		position: fixed;
		bottom: calc(100vh - 150px);
		right: 40px;
		cursor: pointer;
		transition: all 0.3s;
	}
	.handle {
		height: 60px;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		background-color: #fff;
		margin: 20px;
		padding: 0 20px;
		border-radius: 8px;
		box-sizing: border-box;
	}
</style>
